class Point < ActiveRecord::Base
  include HTTParty
  debug_output $stdout

  attr :body, :ev, :parm, :url, :cookie

  def initialize(link_a_str, url, cookie)
    #{ ajaxpost(this,'yy','36|43388|80787|2014-05-05|158|20:00-20:30|15|41'); } return false;">约</a>)
    # -> this,'yy','36|43388|80787|2014-05-05|158|20:00-20:30|15|41'); } return false;">约</a>)
    # -> this,'yy','36|43388|80787|2014-05-05|158|20:00-20:30|15|41'
    @body = link_a_str
    ev, parm = link_a_str.to_s.split('ajaxpost(')[1].split(');')[0].split(',')[1..-1]
    @ev = ev[1..-2]
    @parm = parm[1..-2]

    @url = url
    @cookie = cookie
  end

  def to_hash
    {ev: @ev, parm: @parm}
  end

  def do_appoint
    # /pfs/dr_coaches/post?pxbc=4&isopen=1&sid=36&titem=03&listview=wsyy4yy&jljzlx=37&code=01045207&isbdcl=1&xyid=80787&pdate=2014-05-03&pdate1=2014-05-03&dr_exsy_userclass.ucid3.ddesc=1
    resp = self.class.post(@url.gsub('pfs/dr_coaches', 'pfs/dr_coaches/post'), headers: {'Cookie' => @cookie}, body: to_hash)
    doc = Nokogiri::HTML(resp.body)
    doc.css('td font[color=green]').size == 1
  end

  def to_s
    "#{parm} -> #{body}"
  end
end
